<?php
/**
 * @Notes: 文件描述
 * @Interface getCondition
 * @Return mixed
 * @Author: fc
 * @Time: 2021/4/20 18:11
 */

namespace app\common\service;


use Bluerhinos\phpMQTT as MqttClient;

class subscribeTopic
{

    public static function subscribe($host, $port)
    {
        $username = "";
        $password = "";
        //同样这里少了证书参数
        $mqtt = new MqttClient($host, $port, "ClientID".rand());

        if(!$mqtt->connect(true,NULL,$username,$password)){
            exit(1);
        }

        //currently subscribed topics
        $topics['mytest'] = array("qos"=>0, "function"=>"procmsg");
        $mqtt->subscribe($topics,0);

        while($mqtt->proc()){ //无限去请求

        }

        $mqtt->close();

        //function procmsg($topic,$msg){//输出消息
        //    echo "Msg Recieved: $msg";
        //}
    }

}